/**
 * 简化版测试程序 - 不依赖JUnit
 * 直接运行验证系统功能
 */
public class SimpleTest {
    
    public static void main(String[] args) {
        System.out.println("=== IoT数据处理系统功能测试 ===");
        System.out.println();
        
        boolean allTestsPassed = true;
        
        // 测试1: 协议解析功能
        System.out.println("[测试1] 协议解析功能测试");
        allTestsPassed &= testProtocolParsing();
        
        // 测试2: 数据验证功能
        System.out.println("[测试2] 数据验证功能测试");
        allTestsPassed &= testDataValidation();
        
        // 测试3: 异常处理功能
        System.out.println("[测试3] 异常处理功能测试");
        allTestsPassed &= testExceptionHandling();
        
        // 测试4: 消息队列功能
        System.out.println("[测试4] 消息队列功能测试");
        allTestsPassed &= testMessageQueue();
        
        // 输出测试结果
        System.out.println();
        System.out.println("=== 测试结果总结 ===");
        if (allTestsPassed) {
            System.out.println("✅ 所有测试通过！系统功能正常");
        } else {
            System.out.println("❌ 部分测试失败，请检查系统配置");
        }
    }
    
    /**
     * 测试协议解析功能
     */
    private static boolean testProtocolParsing() {
        try {
            String testMessage = "0000000161C0A5F009F6177F";
            
            // 解析设备ID
            String deviceIdHex = testMessage.substring(0, 8);
            long deviceId = Long.parseUnsignedLong(deviceIdHex, 16);
            
            // 解析时间戳
            String timestampHex = testMessage.substring(8, 16);
            long timestamp = Long.parseUnsignedLong(timestampHex, 16);
            
            // 解析温度
            String tempHex = testMessage.substring(16, 20);
            int tempInt = Integer.parseUnsignedInt(tempHex, 16);
            double temperature = tempInt / 100.0;
            
            // 解析湿度
            String humidityHex = testMessage.substring(20, 24);
            int humidityInt = Integer.parseUnsignedInt(humidityHex, 16);
            double humidity = humidityInt / 100.0;
            
            // 验证解析结果
            boolean deviceIdCorrect = (deviceId == 1);
            boolean temperatureCorrect = (Math.abs(temperature - 25.50) < 0.01);
            boolean humidityCorrect = (Math.abs(humidity - 60.15) < 0.01);
            
            System.out.println("  输入消息: " + testMessage);
            System.out.println("  设备ID: " + deviceId + " " + (deviceIdCorrect ? "✅" : "❌"));
            System.out.println("  温度: " + temperature + "°C " + (temperatureCorrect ? "✅" : "❌"));
            System.out.println("  湿度: " + humidity + "% " + (humidityCorrect ? "✅" : "❌"));
            
            boolean passed = deviceIdCorrect && temperatureCorrect && humidityCorrect;
            System.out.println("  结果: " + (passed ? "✅ 通过" : "❌ 失败"));
            System.out.println();
            
            return passed;
            
        } catch (Exception e) {
            System.out.println("  ❌ 测试异常: " + e.getMessage());
            System.out.println();
            return false;
        }
    }
    
    /**
     * 测试数据验证功能
     */
    private static boolean testDataValidation() {
        try {
            // 测试正常数据
            boolean normalDataValid = validateSensorData(25.0, 60.0);
            
            // 测试边界数据
            boolean boundaryDataValid = validateSensorData(-50.0, 0.0) && validateSensorData(80.0, 100.0);
            
            // 测试异常数据
            boolean invalidDataRejected = !validateSensorData(-100.0, 150.0);
            
            System.out.println("  正常数据验证: " + (normalDataValid ? "✅ 通过" : "❌ 失败"));
            System.out.println("  边界数据验证: " + (boundaryDataValid ? "✅ 通过" : "❌ 失败"));
            System.out.println("  异常数据拒绝: " + (invalidDataRejected ? "✅ 通过" : "❌ 失败"));
            
            boolean passed = normalDataValid && boundaryDataValid && invalidDataRejected;
            System.out.println("  结果: " + (passed ? "✅ 通过" : "❌ 失败"));
            System.out.println();
            
            return passed;
            
        } catch (Exception e) {
            System.out.println("  ❌ 测试异常: " + e.getMessage());
            System.out.println();
            return false;
        }
    }
    
    /**
     * 测试异常处理功能
     */
    private static boolean testExceptionHandling() {
        try {
            int successCount = 0;
            int totalTests = 4;
            
            // 测试1: 消息长度错误
            try {
                parseMessage("12345"); // 长度不足
                System.out.println("  长度错误处理: ❌ 应该抛出异常");
            } catch (Exception e) {
                System.out.println("  长度错误处理: ✅ 正确抛出异常");
                successCount++;
            }
            
            // 测试2: 非法十六进制字符
            try {
                parseMessage("GGGGGGGG61C0A5F009F6177F"); // 包含非法字符G
                System.out.println("  非法字符处理: ❌ 应该抛出异常");
            } catch (Exception e) {
                System.out.println("  非法字符处理: ✅ 正确抛出异常");
                successCount++;
            }
            
            // 测试3: 空消息
            try {
                parseMessage(null);
                System.out.println("  空消息处理: ❌ 应该抛出异常");
            } catch (Exception e) {
                System.out.println("  空消息处理: ✅ 正确抛出异常");
                successCount++;
            }
            
            // 测试4: 空字符串
            try {
                parseMessage("");
                System.out.println("  空字符串处理: ❌ 应该抛出异常");
            } catch (Exception e) {
                System.out.println("  空字符串处理: ✅ 正确抛出异常");
                successCount++;
            }
            
            boolean passed = (successCount == totalTests);
            System.out.println("  结果: " + (passed ? "✅ 通过" : "❌ 失败") + " (" + successCount + "/" + totalTests + ")");
            System.out.println();
            
            return passed;
            
        } catch (Exception e) {
            System.out.println("  ❌ 测试异常: " + e.getMessage());
            System.out.println();
            return false;
        }
    }
    
    /**
     * 测试消息队列功能
     */
    private static boolean testMessageQueue() {
        try {
            java.util.concurrent.BlockingQueue<String> queue = 
                new java.util.concurrent.LinkedBlockingQueue<>(10);
            
            // 测试队列基本操作
            boolean offerSuccess = queue.offer("test-message-1");
            boolean offerSuccess2 = queue.offer("test-message-2");
            
            String message1 = queue.poll();
            String message2 = queue.poll();
            
            boolean queueWorking = offerSuccess && offerSuccess2 && 
                                 "test-message-1".equals(message1) && 
                                 "test-message-2".equals(message2);
            
            System.out.println("  队列基本操作: " + (queueWorking ? "✅ 通过" : "❌ 失败"));
            
            // 测试队列容量限制
            boolean capacityTest = true;
            for (int i = 0; i < 15; i++) {
                if (!queue.offer("message-" + i)) {
                    capacityTest = (i >= 10); // 应该在第10个消息时开始失败
                    break;
                }
            }
            
            System.out.println("  队列容量限制: " + (capacityTest ? "✅ 通过" : "❌ 失败"));
            
            boolean passed = queueWorking && capacityTest;
            System.out.println("  结果: " + (passed ? "✅ 通过" : "❌ 失败"));
            System.out.println();
            
            return passed;
            
        } catch (Exception e) {
            System.out.println("  ❌ 测试异常: " + e.getMessage());
            System.out.println();
            return false;
        }
    }
    
    /**
     * 验证传感器数据
     */
    private static boolean validateSensorData(double temperature, double humidity) {
        return temperature >= -50 && temperature <= 80 && 
               humidity >= 0 && humidity <= 100;
    }
    
    /**
     * 解析消息（简化版本）
     */
    private static void parseMessage(String message) {
        if (message == null || message.isEmpty()) {
            throw new IllegalArgumentException("消息不能为空");
        }
        
        if (message.length() != 24) {
            throw new IllegalArgumentException("消息长度必须为24位");
        }
        
        // 验证是否为有效的十六进制字符
        for (char c : message.toCharArray()) {
            if (!Character.isDigit(c) && !(c >= 'A' && c <= 'F') && !(c >= 'a' && c <= 'f')) {
                throw new IllegalArgumentException("包含非法十六进制字符: " + c);
            }
        }
        
        // 尝试解析各个部分
        Long.parseUnsignedLong(message.substring(0, 8), 16);    // 设备ID
        Long.parseUnsignedLong(message.substring(8, 16), 16);   // 时间戳
        Integer.parseUnsignedInt(message.substring(16, 20), 16); // 温度
        Integer.parseUnsignedInt(message.substring(20, 24), 16); // 湿度
    }
}